package com.pox.guli.service.cms.controller.api;

import com.pox.guli.common.base.result.R;
import com.pox.guli.service.cms.entity.Ad;
import com.pox.guli.service.cms.service.AdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author PoX21s
 * @Date: 2022/1/5 15:17
 * @Version 1.0
 */

@Slf4j
@Api(description = "广告推荐")
@RestController
@RequestMapping("/api/cms/ad")
//@CrossOrigin
public class ApiAdController {

    @Resource
    private AdService adService;
    @Resource
    private RedisTemplate redisTemplate;

    @ApiOperation("根据推荐位id显示广告推荐")
    @GetMapping("/list/{adTypeId}")
    public R listByAdTypeId(@ApiParam(value = "推荐位id", required = true) @PathVariable String adTypeId) {

        List<Ad> ads = adService.selectByAdTypeId(adTypeId);

        return R.ok().data("items", ads);
    }

    @ApiOperation("redis保存测试")
    @PostMapping("/save-test")
    public R saveAd(@RequestBody Ad ad){

        //redisTemplate.opsForValue().set("ad1", ad);
        redisTemplate.opsForValue().set("index::ad", ad);

        return R.ok();
    }

    @ApiOperation("redis查找测试")
    @GetMapping("/get-test/{key}")
    public R getAd(@PathVariable String key){

        Ad ad = (Ad)redisTemplate.opsForValue().get(key);

        return R.ok().data("ad", ad);
    }

    @ApiOperation("redis删除测试")
    @DeleteMapping("/remove-test/{key}")
    public R removeAd(@PathVariable String key){

        Boolean delete = redisTemplate.delete(key);
        //是否删除成功
        System.out.println(delete);
        //key是否存在
        Boolean hasKey = redisTemplate.hasKey(key);
        System.out.println(hasKey);

        return R.ok();
    }
}
